package com.example.demo.common;

import com.example.demo.utils.IdUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
public class R<T> {
    private Integer code;
    private T data;
    private String msg;
    private String reqId;
    private String time;
    private Long timestamp;

    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setData(data);
        r.setMsg("success");
        r.setReqId(IdUtils.randomUUID());
        Date date = new Date();
        r.setTime(date.toString());
        r.setTimestamp(date.getTime());
        return r;
    }

    public R(int code, String msg) {
        this.code = code;
        this.data = null;
        this.msg = msg;
        this.reqId = IdUtils.randomUUID();
        Date date = new Date();
        this.time = date.toString();
        this.timestamp = date.getTime();
    }
}
